package com.biostime.qdingding.modules.qd.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.qdingding.common.config.Global;
import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.qd.entity.Courseprice;
import com.biostime.qdingding.modules.qd.service.CoursepriceService;
import com.biostime.qdingding.modules.sys.entity.User;
import com.biostime.qdingding.modules.sys.utils.UserUtils;

/**
 * 课程价格Controller
 * @author king
 * @version 2015-11-18
 */
@Controller
@RequestMapping(value = "${adminPath}/qd/courseprice")
public class CoursepriceController extends BaseController {

	@Autowired
	private CoursepriceService coursepriceService;
	
	@ModelAttribute
	public Courseprice get(@RequestParam(required=false) String id) {
		Courseprice entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = coursepriceService.get(id);
		}
		if (entity == null){
			entity = new Courseprice();
		}
		return entity;
	}
	
	@RequiresPermissions("qd:courseprice:view")
	@RequestMapping(value = {"list", ""})
	public String list(Courseprice courseprice, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Courseprice> page = coursepriceService.findPage(new Page<Courseprice>(request, response), courseprice); 
		model.addAttribute("page", page);
		return "modules/qd/coursepriceList";
	}

	@RequiresPermissions("qd:courseprice:view")
	@RequestMapping(value = "form")
	public String form(Courseprice courseprice, Model model) {
		courseprice.setType(0);
		model.addAttribute("courseprice", courseprice);
		return "modules/qd/coursepriceForm";
	}

	@RequiresPermissions(value={"qd:courseprice:edit","qd:courseprice:add"}, logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(Courseprice courseprice, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, courseprice)){
			return form(courseprice, model);
		}
		
		User curUser = UserUtils.getUser();
		courseprice.setCenter(curUser.getCenter());
		
		//中心只能存在一个忙时价格
		if(courseprice.getType() == 0){
				if(coursepriceService.onlyOne(courseprice).size() == 0){
					coursepriceService.save(courseprice);
					addMessage(redirectAttributes, "保存成功");
				}else{
					if(courseprice.getId() != null && !courseprice.getId().equals("")){
						coursepriceService.save(courseprice);
						addMessage(redirectAttributes, "保存成功");
					}else{
						addMessage(redirectAttributes, "中心已存在忙时价格");
					}
				}
				}else{
			coursepriceService.save(courseprice);
			addMessage(redirectAttributes, "保存成功");
		}
		
		return "redirect:"+Global.getAdminPath()+"/qd/courseprice/?repage";
	}
	
	@RequiresPermissions("qd:courseprice:edit")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Courseprice courseprice, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				coursepriceService.delete(new Courseprice(id));
			}
		}else{
			coursepriceService.delete(courseprice);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/qd/courseprice/?repage";
	}

}